/*
 * Copyright (c) 2014 Luca Agostini, Alessandro Cantini, Alessandro Dignani
 *
 * This file is part of MeteoCal.
 *
 * MeteoCal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.polimi.se2.agostini_cantini_dignani.meteocal.entity;

import it.polimi.se2.agostini_cantini_dignani.meteocal.model.TemperatureUnit;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
 * @author Alessandro Cantini
 */
@Entity
public class WeatherForecast implements Serializable {

	public static final TemperatureUnit ENTITY_TEMPERATURE_UNIT = TemperatureUnit.KELVIN;

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer weatherForecastId;

	@NotNull
	@Size(min = 1, max = 30)
	private String weather;

	@DecimalMin("0.0")
	private Float temperature;

	private String iconId;

	public WeatherForecast() {
	}

	public WeatherForecast(String weather) {
		this.weather = weather;
	}

	public Integer getWeatherForecastId() {
		return weatherForecastId;
	}

	public void setWeatherForecastId(Integer weatherForecastId) {
		this.weatherForecastId = weatherForecastId;
	}

	public String getWeather() {
		return weather;
	}

	public void setWeather(String weather) {
		this.weather = weather;
	}

	public Float getTemperature() {
		return temperature;
	}

	public void setTemperature(Float temperature) {
		if (temperature < 0.0f) {
			throw new IllegalArgumentException();
		}
		this.temperature = temperature;
	}

	public String getIconId() {
		return iconId;
	}

	public void setIconId(String iconId) {
		this.iconId = iconId;
	}

	/**
	 * Tells if the forecast is the same of another forecast.
	 *
	 * @param wf the weather forecast to compare.
	 * @return true if the forecast is equivalent to the other forecast, false otherwise.
	 */
	public boolean hasSameWeather(WeatherForecast wf) {
		if (wf == null) {
			throw new IllegalArgumentException();
		}
		return hasSameWeather(wf.getWeather());
	}

	/**
	 * * Tells if the forecast is the same of a given string.
	 *
	 * @param weather the string representing the weather.
	 * @return true if the forecast is equivalent to the other forecast, false otherwise.
	 */
	public boolean hasSameWeather(String weather) {
		if (weather == null) {
			throw new IllegalArgumentException();
		}
		return this.getWeather().equalsIgnoreCase(weather);
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (weatherForecastId != null ? weatherForecastId.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof WeatherForecast)) {
			return false;
		}
		WeatherForecast other = (WeatherForecast) object;
		if ((this.weatherForecastId == null && other.weatherForecastId != null) || (this.weatherForecastId != null && !this.weatherForecastId.equals(other.weatherForecastId))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "WeatherForecast{" +
				"weatherForecastId=" + weatherForecastId +
				", weather='" + weather + '\'' +
				", temperature=" + temperature +
				'}';
	}
}
